package com.dzm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dzm.entity.Advertisement;
import com.dzm.entity.Category;
import com.dzm.mapper.CategoryMapper;
import com.dzm.service.AdvertisementService;
import com.dzm.mapper.AdvertisementMapper;
import com.dzm.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Dzm
* @description 针对表【advertisement】的数据库操作Service实现
* @createDate 2024-08-31 09:41:17
*/
@Service
public class AdvertisementServiceImpl extends ServiceImpl<AdvertisementMapper, Advertisement>
    implements AdvertisementService{

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<Advertisement> searchAdver(Integer pageNum, Integer pageSize, SearchVo vo) {
        LambdaQueryWrapper<Advertisement> wrapper = new LambdaQueryWrapper<>();
        if(vo != null) {
            wrapper.like(StrUtil.isNotBlank(vo.getTitle()), Advertisement::getTitle, vo.getTitle())
                    .eq(vo.getCateId() != null && vo.getCateId() != 0, Advertisement::getCategoryId, vo.getCateId());
        }

        Page<Advertisement> page = this.page(new Page<>(pageNum, pageSize), wrapper);

        List<Category> list = categoryMapper.selectList(null);
        for (Advertisement adver : page.getRecords()) {
            for (Category cate : list) {
                if(adver.getCategoryId().equals(cate.getId())) {
                    adver.setCateName(cate.getName());
                }
            }
        }

        return page;
    }
}




